﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Netcached.Tests
{
    [TestClass]
    public class TestCas : BaseTestClass
    {
        [TestMethod]
        public void TestChangeOriginal()
        {
            string key = "TestCas.TestChangeOriginal";
            string value = "94385zu39iufgh58k58ws9835jv85";
            string value2 = "rwet42339iudsfgsd58k4564ghrfgjv85";

            var initialOpResult = Client.Set(key, value);

            ulong casUnique;
            string fetchedValue = Client.Get<string>(key, out casUnique);

            var casResult = Client.CheckAndSet(key, value2, casUnique);

            Assert.IsTrue(initialOpResult);
            Assert.IsTrue(casUnique != 0);
            Assert.IsTrue(casResult == CasResult.Stored);
        }

        [TestMethod]
        public void TestChangeModified()
        {
            string key = "TestCas.TestChangeModified";
            string value = "94385zu39iufgh58k58ws9835jv85";
            string value2 = "rwet42339iudsfgsd58k4564ghrfgjv85";
            string value3 = "3gtzewteutezrdsawtz6ziukhjdbvsewr";

            var initialOpResult = Client.Set(key, value);

            ulong casUnique;
            string fetchedValue = Client.Get<string>(key, out casUnique);

            var subsequentOpResult = Client.Set(key, value2);

            ulong subsequentCasUnique;
            Client.Get<string>(key, out subsequentCasUnique);

            var casResult = Client.CheckAndSet(key, value3, casUnique);

            Assert.IsTrue(initialOpResult);
            Assert.IsTrue(casUnique != 0);
            Assert.IsTrue(subsequentOpResult);
            Assert.IsTrue(casResult == CasResult.ErrorModified);
            Assert.AreNotEqual(casUnique, subsequentOpResult);
        }
    }
}
